package com.onetech.adrewards.free.dialogs;

import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.fragments.ExchangeFragment.ExchangeHandler;
import com.onetech.adrewards.free.http.APIRequestResult;
import com.onetech.adrewards.free.models.GiftCardModel;
import com.onetech.adrewards.free.utils.AdrewadsUtils;
import com.todddavies.components.progressbar.ProgressWheel;
import com.zuzu.dialogs.BaseDialog;

public class ExchangeProcessDialog extends BaseDialog {
	private Activity activity = null;
	private ProgressWheel mProgressBar = null;
	private View mExchangerContainer = null;
	private TextView mExchangeTxt = null;
	private String uId="";
	private int type=0;
	private ExchangeProcess mExchangeProcess=null;
	private ExchangeHandler mExchangeHandle=null;
	private Tracker mTracker;
	
	public ExchangeProcessDialog(Activity _activity,String _uId,int _type,ExchangeHandler _handler) {
		super(_activity, _activity.getString(R.string.exchange_process_dialog_title),
				TYPE_NO_BUTTON, R.layout.exchange_process_layout);
		activity = _activity;
		mExchangeHandle=_handler;
		uId=_uId;
		type=_type;
		mExchangerContainer = (LinearLayout) findViewById(R.id.exchangeContainer);
		mExchangeTxt = (TextView) findViewById(R.id.exchangeProcessMessage);
		mProgressBar=(ProgressWheel)findViewById(R.id.exchangeProgressBar);
		mProgressBar.spin();
		showHideProgressBar(true);
		doExchangeNow();
		
	}
	private void showHideProgressBar(boolean show){
		if(show){
			mProgressBar.setVisibility(View.VISIBLE);
			mExchangerContainer.setVisibility(View.GONE);
		}
		else{
			mProgressBar.setVisibility(View.GONE);
			mExchangerContainer.setVisibility(View.VISIBLE);
		}
	}
	private void doExchangeNow(){
		if(mExchangeProcess==null || mExchangeProcess.getStatus()==Status.FINISHED){
			mExchangeProcess=new ExchangeProcess();
			mExchangeProcess.execute();
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		default:
			super.onClick(v);
			break;
		}
	}

	private class ExchangeProcess extends
			AsyncTask<Void, Void, APIRequestResult> {
		@Override
		protected void onPreExecute() {		
			super.onPreExecute();
			showHideProgressBar(true);
		}
		@Override
		protected APIRequestResult doInBackground(Void... params) {
			try {
				Bundle mParams=new Bundle();
				mParams.putString("mem_id", uId);
				mParams.putString("card_type", String.valueOf(type));
				return GiftCardModel.getInstance().exchangeProcess(mParams);
			} catch (Exception e) {
				return null;
			}
		}

		@Override
		protected void onPostExecute(APIRequestResult result) {
			super.onPostExecute(result);
			if(result!=null){
				try {
					if (result.error_code.trim().equals("0") && result.data != null && !result.data.equals("")) {
						JSONObject jb=(JSONObject)result.data;
						String giftCode=jb.getJSONObject("result").getString("gift_card");
						mExchangeTxt.setText(giftCode);
						mExchangeHandle.reLoadFragment(true, true, true);
					}else{
						String error_message=result.error_msg;
						//AdrewadsUtils.showMessage(getContext(), error_message);
						AdrewadsUtils.showInfoDialog(getContext(), "", error_message);
						dismiss();
					}					
				} catch (Exception e) {
					AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.exchange_failed_error_message));
					dismiss();
				}
			}else{
				AdrewadsUtils.showInfoDialog(getContext(), "", getContext().getString(R.string.exchange_failed_error_message));
				dismiss();
			}
			showHideProgressBar(false);
		}
	}
	@Override
    public void onStart() {
        super.onStart();
        try {
            mTracker.set(Fields.SCREEN_NAME, activity.getString(R.string.ExchangeProcessDialog));            
            mTracker.send(MapBuilder.createAppView().build());            
		} catch (Exception e) {
		}
    }
}
